package face.pay.api.order.controller;

import com.alibaba.fastjson.JSON;
import com.face.pay.IPayConfigServer;
import com.face.pay.WxApiConfig;
import com.github.wxpay.sdk.WXPayConstants;
import com.github.wxpay.sdk.WXPayRequest;
import com.github.wxpay.sdk.WXPayUtil;
import face.pay.entity.EnterpriseInfo;
import face.pay.entity.SharingOrderInfo;
import face.pay.util.FaceStringBuffer;
import face.pay.util.ReadFromFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分账
 */
@Controller
@Slf4j
@RequestMapping("order/test")
public class SharingOrderBiz {


    private final static   String domain    = "api.mch.weixin.qq.com";

    /**
     * secapi/pay/profitsharing
     */
    private final static   String urlSuffix = "/secapi/pay/profitsharing";


    @Autowired
    public IPayConfigServer iWxPayConfigServer;

    @RequestMapping("/sharing")
    public Boolean sharingAccount(){

        log.info("开始分账");
        Boolean success = false;
        List<SharingOrderInfo> sharingOrderInfos = new ArrayList<>();

        WxApiConfig wxApiConfig = this.iWxPayConfigServer.getConfig(11);

        try{

            WXPayRequest wxPayRequest = new WXPayRequest(wxApiConfig);


            Map<String, String> map = new HashMap<>();
            map.put("mch_id", "1501976041");
            map.put("nonce_str", "e0wtTdP2lYhDlAjpfQfXbgNbM3bg9b1G");
            map.put("sub_mch_id", "1558575011");
            map.put("appid", "wx39fbc8a193237d56");
            map.put("transaction_id", "4200000455202001015077202830");




            map.put("out_order_no", "87765d25-2516-4ae4-b88d-db80fec4e148");
            List<Map<String, Object>> receivers = new ArrayList<>();
            Map<String, Object> receiver = new HashMap<>();
            receiver.put("type", "MERCHANT_ID");
            receiver.put("account", "1501594551");
            receiver.put("amount", Long.parseLong("2"));
            receiver.put("description", "优惠券核销服务费");
            receivers.add(receiver);
            String REJSON  = JSON.toJSONString(receivers);// ReadFromFile.read("D://SHANRING.txt","GBK");
            map.put("receivers", REJSON);
            String xml = WXPayUtil.generateSignedXml(map, wxApiConfig.getKey(), WXPayConstants.SignType.HMACSHA256);
            log.info("发送请求 " + xml);

            String result = wxPayRequest.requestOnce(domain, urlSuffix, xml, true);
            log.info("返回结果 " + result);


        }catch (Exception e){
            success =  false;
            log.error("分账异常",e);
        }
        return  success;

    }

    public  void setResult(Boolean isSuccess,String sharingMessage,List<SharingOrderInfo> list){
        for(SharingOrderInfo sharingOrderInfo: list){
            String ms = sharingOrderInfo.getMessage();

            FaceStringBuffer msg = new FaceStringBuffer(ms);
            msg.add(sharingMessage);

            sharingOrderInfo.setMessage(msg.toString());
            if(isSuccess){
                sharingOrderInfo.setStatus(1);
            }else{
                sharingOrderInfo.setStatus(-1);

            }

        }

    }

}
